package zhou.chs.p2_12;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    TextView tv1,tv2;   //显示开度设定值
    Button bt1,bt2;     //切换界面按钮
    String strb;
    int sn;
    int kn1=0,kn2=0;
    int kn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1=(TextView)findViewById(R.id.idtv1);  //控件变量赋值
        tv2=(TextView)findViewById(R.id.idtv2);
        bt1=(Button)findViewById(R.id.idbt1);
        bt2=(Button)findViewById(R.id.idbt2);
        bt1.setOnClickListener(this);           //注册监听按钮单击事件
        bt2.setOnClickListener(this);
    }
    //开度设定界面关闭后返回参数处理
    protected void onActivityResult(int requestCode,int resultCode,Intent it){
        if(resultCode == RESULT_OK){
            int n = it.getIntExtra("开度",0);
            if(sn==1) {     //获取1#调节阀开度设定值并显示
                tv1.setText("开度设定：" + n);
                kn1=n;
            }
            if(sn==2) {    //获取2#调节阀开度设定值并显示
                tv2.setText("开度设定：" + n);
                kn2=n;
            }
        }
    }
    @Override  //按钮单击事件响应
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.idbt1:           //1#调节阀
                strb="1#调节阀";
                sn=1;
                kn=kn1;
                break;
            case R.id.idbt2:           //2#调节阀
                strb="2#调节阀";
                sn=2;
                kn=kn2;
                break;
        }
        Intent it = new Intent(this,Main2Activity.class);
        it.putExtra("设备",strb);  //启动开度设定界面前先放入待传送参数
        it.putExtra("开度",kn);
        startActivityForResult(it,sn);  //启动开度设定界面
    }
}
